package org.cmg.study.spring.config;

import org.cmg.study.spring.entity.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * @author CMG
 * @version 1.0
 * @cLassName UserConvert
 * @description
 * @date 2021/9/6 8:57
 **/
@Component
public class UserConvert implements Converter<String, User> {
    @Override
    public User convert(String s) {
        //创建实例
        User user = new User();
        System.out.println("接收数据："+  s);
        //以“，”分隔
        String []stringValues = s.split(",");
        if(null != stringValues && stringValues.length ==3){
            //为user实例赋值
            user.setName(stringValues[0]);
            user.setAge(Integer.valueOf(stringValues[1]));
            user.setHeight(Double.valueOf(stringValues[2]));
            return user;
        }else{
            throw new IllegalArgumentException(String.format("类型转换失败，需要格式‘编程帮，18，1.85’，但是格式是[%s]", s));
        }
    }
}
